package org.zero.onlinesubject.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.zero.onlinesubject.common.constant.RedisConstant;
import org.zero.onlinesubject.common.param.TagTypeCount;
import org.zero.onlinesubject.common.vo.TagVo;
import org.zero.onlinesubject.entity.SystemFile;
import org.zero.onlinesubject.entity.TagType;
import org.zero.onlinesubject.mapper.SystemFileMapper;
import org.zero.onlinesubject.service.TagTypeService;
import org.zero.onlinesubject.mapper.TagTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TagTypeServiceImpl extends ServiceImpl<TagTypeMapper, TagType>
    implements TagTypeService{

    @Value("${file.external_path}")
    private String externalPath;

    @Resource
    private TagTypeMapper tagTypeMapper;

    @Resource
    private SystemFileMapper systemFileMapper;

    @Resource
    private Executor taskExecutor;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<TagVo> getTopTag() throws ExecutionException, InterruptedException {
        String json = stringRedisTemplate.opsForValue().get(RedisConstant.TAG_TYPE_PREFIX);
        if(!StringUtils.isNullOrEmpty(json)){
            return JSON.parseObject(json,new TypeReference<List<TagVo>>(){});
        }

        QueryWrapper<TagType> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("ClickCount").last("limit 0,6");
        List<TagType> tagTypes = tagTypeMapper.selectList(wrapper);

        CompletableFuture<Map<Integer, String>> f1 = CompletableFuture.supplyAsync(() -> {
            List<Integer> tagIds = tagTypes.stream().map(TagType::getId).collect(Collectors.toList());

            List<SystemFile> list = systemFileMapper.selectByTagId(tagIds);
            return list.stream().peek(info->{ info.setFilepath(externalPath + info.getFilepath());}).collect(Collectors.toMap(SystemFile::getId, SystemFile::getFilepath));
        }, taskExecutor);

        CompletableFuture<Map<String, Integer>> f2 = CompletableFuture.supplyAsync(() -> {
            List<String> codes = tagTypes.stream().map(TagType::getTypecode).collect(Collectors.toList());
            List<TagTypeCount> countList = tagTypeMapper.selectCourseCount(codes);
            return countList.stream().collect(Collectors.toMap(TagTypeCount::getTypecode, TagTypeCount::getCount));
        }, taskExecutor);
        List<TagVo> tagVos = copyTagVo(tagTypes);

        CompletableFuture.allOf(f1,f2).get();
        Map<Integer, String> pathMap = f1.get();
        Map<String, Integer> countMap = f2.get();

        List<TagVo> voList = tagVos.stream().map(vo -> {
            vo.setTagcover(pathMap.get(vo.getId()));
            vo.setCourseCount(countMap.get(vo.getTypecode()));
            return vo;
        }).collect(Collectors.toList());
        stringRedisTemplate.opsForValue().set(RedisConstant.TAG_TYPE_PREFIX, JSON.toJSONString(voList),5, TimeUnit.MINUTES);
        return voList;
    }

    private List<TagVo> copyTagVo(List<TagType> tagTypes){
        List<TagVo> voList = new ArrayList<>();
        for (TagType tagType : tagTypes) {
            TagVo tagVo = new TagVo();
            BeanUtils.copyProperties(tagType,tagVo);
            voList.add(tagVo);
        }
        return voList;
    }
}




